package com.sinfonia.wipes.common.utils.phantom.taobao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.sinfonia.wipes.common.constant.IPlatformConstant;
import com.sinfonia.wipes.entity.taobao.TaobaoListItem;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by gmwang on 2018/10/17
 * 抓取工具类
 * @Author gmwang
 */
public class GrabTaobaoUtils {
    /**
     * 抓取pc端 uland.taobao.com
     * @param params
     */
    public static List<TaobaoListItem> searchItemPCULAND(Map<String,Object> params) throws UnsupportedEncodingException {
        //设置必要参数
        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl证书支持
        dcaps.setCapability("acceptSslCerts", true);
        //截屏支持
        dcaps.setCapability("takesScreenshot", true);
        //css搜索支持
        dcaps.setCapability("cssSelectorsEnabled", true);
        //js支持
        dcaps.setJavascriptEnabled(true);
        //驱动支持（第二参数表明的是你的phantomjs引擎所在的路径，使用whereis phantomjs可以查看）
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\dev\\tools\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
        //创建无界面浏览器对象
        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        //设置隐性等待
        driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String url = IPlatformConstant.TAOBAO_ULAND_SEARCH_URL.replace("TB_KEYWORD",URLEncoder.encode(params.get("keyWord")+"","utf-8")).replace("PAGENUM",params.get("pageNum")+"");
        //获取内容
        driver.get(url);
        System.out.println("===start0================="+url);
        TaobaoListItem taobaoListItem;
        List<WebElement> list = driver.findElementsByClassName("item");
        List<TaobaoListItem> listItem = new ArrayList<>();
        int start = 0;
        int num = Integer.parseInt(StringUtils.isBlank(params.get("pageSize")+"")?"50":params.get("pageSize")+"");
        for (WebElement item:list ) {
            if(start++ > num){
                break;
            }
            taobaoListItem = new TaobaoListItem();
            String goodsUrl = item.findElements(By.tagName("a")).get(0).getAttribute("href");
            String price = item.findElements(By.tagName("strong")).get(0).getText();
            String title = item.findElements(By.className("title")).get(0).getAttribute("title");
            List<WebElement> shopNick1 = item.findElements(By.className("shopNick"));
            String shopNick = "";
            if(!shopNick1.isEmpty()){
                shopNick = item.findElements(By.className("shopNick")).get(0).getText();
            }
            String payNum = item.findElements(By.className("payNum")).get(0).getText();
            String imgUrl = item.findElements(By.tagName("img")).get(0).getAttribute("src");
            List<WebElement> elementList = item.findElements(By.className("postalicon"));
            taobaoListItem.setName(title);
            taobaoListItem.setUrl(goodsUrl);
            taobaoListItem.setDescription(title);
            taobaoListItem.setPrice(price);
            taobaoListItem.setStoreName(shopNick);
            taobaoListItem.setPayersNum(payNum);
            taobaoListItem.setImgUrl(imgUrl);
            if(elementList.isEmpty()){
                taobaoListItem.setByShipping(2);
            }else{
                taobaoListItem.setByShipping(1);
            }
            listItem.add(taobaoListItem);
        }
        System.out.println("抓取到多个商品："+list.size());
        return listItem;
    }
//    /**
//     * 需要登录，暂时不做
//     * 抓取pc端 s.taobao.com
//     * @param params
//     */
//    public static void searchItemPCS(Map<String,Object> params){
//        //设置必要参数
//        DesiredCapabilities dcaps = new DesiredCapabilities();
//        //ssl证书支持
//        dcaps.setCapability("acceptSslCerts", true);
//        //截屏支持
//        dcaps.setCapability("takesScreenshot", true);
//        //css搜索支持
//        dcaps.setCapability("cssSelectorsEnabled", true);
//        //js支持
//        dcaps.setJavascriptEnabled(true);
//        //驱动支持（第二参数表明的是你的phantomjs引擎所在的路径，使用whereis phantomjs可以查看）
////        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/usr/local/bin/phantomjs");
//        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\dev\\tools\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
//        //创建无界面浏览器对象
//        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
//        //设置隐性等待
//        driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        String url = IPlatformConstant.TAOBAO_ULAND_SEARCH_URL.replace("TB_KEYWORD",params.get("keyword")+"").replace("PAGENUM",params.get("pageNum")+"");
//        //获取内容
//        driver.get(url);
//        System.out.println("==============================start0==============================");
//        ArrayList<Object> list1 = new ArrayList<>();
//        List<WebElement> list = driver.findElementsByClassName("item");
//        list.forEach(item ->{
//            By by = By.tagName("a");
//            By by2 = By.tagName("strong");
//            By by3 = By.className("title");
//            String href = item.findElements(by).get(0).getAttribute("href");
//            String price = item.findElements(by2).get(0).getText();
//            String title = item.findElements(by3).get(0).getAttribute("title");
//            System.out.println("href====="+href);
//            System.out.println("price====="+price);
//            System.out.println("title====="+title);
//            list1.add(1);
//        });
//        System.out.println("抓取到多个商品："+list1.size());
//    }
    /**
     * 抓取手机端H5页面
     * @param params
     */
    public static void searchItemH5(Map<String,Object> params){
        //设置必要参数
        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl证书支持
        dcaps.setCapability("acceptSslCerts", true);
        //截屏支持
        dcaps.setCapability("takesScreenshot", true);
        //css搜索支持
        dcaps.setCapability("cssSelectorsEnabled", true);
        //js支持
        dcaps.setJavascriptEnabled(true);
        //驱动支持（第二参数表明的是你的phantomjs引擎所在的路径，使用whereis phantomjs可以查看）
//        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/usr/local/bin/phantomjs");
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\dev\\tools\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
        //创建无界面浏览器对象
        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        //设置隐性等待
        driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String url = IPlatformConstant.TAOBAO_H5_SEARCH_URL.replace("TB_KEYWORD",params.get("keyword")+"").replace("PAGENUM",params.get("pageNum")+"")
                        .replace("PAGESIZE",(params.get("pageSize")==null?20:params.get("pageSize"))+"");
        //获取内容
        driver.get(url);
        System.out.println("==============================start0==============================");
        List<WebElement> list = driver.findElementsByClassName("list-item");
        TaobaoListItem taobaoListItem;
        for (WebElement item:list ) {
            taobaoListItem = new TaobaoListItem();
            String goodsUrl = item.findElements(By.tagName("a")).get(0).getAttribute("href");
            String imgUrl = item.findElements(By.tagName("img")).get(0).getAttribute("src");
            String price = item.findElements(By.className("font-num")).get(0).getText();
            String title = item.findElements(By.className("d-title")).get(0).getText();
            String area = item.findElements(By.className("d-area")).get(0).getText();
            String payNum = item.findElements(By.className("d-num")).get(0).getText();
            String byShipping = item.findElements(By.className("d-freight")).get(0).getText();
            taobaoListItem.setName(title);
            taobaoListItem.setUrl(goodsUrl);
            taobaoListItem.setDescription(title);
            taobaoListItem.setPrice(price);
            taobaoListItem.setPayersNum(payNum);
            taobaoListItem.setImgUrl(imgUrl);
            taobaoListItem.setShipArea(area);
            if ("免运费".equals(byShipping)) {
                taobaoListItem.setByShipping(1);
            } else {
                byShipping.replace("运费","");
                taobaoListItem.setByShipping(2);
                taobaoListItem.setPostage(byShipping);
            }
        }
        System.out.println("抓取到多个商品："+list.size());
    }
    /**
     * 获取关键词联想
     * 事实证明,pc和手机端联想结果一致，就调用手机端
     * @param params
     */
    public static List associativeWord(Map<String,Object> params) throws IOException {
        List<String> list = new ArrayList<>();
        String keyWord = params.get("keyWord")+"";
        String url = IPlatformConstant.TAOBAO_PC_ASSOCIATIVE_WORD_URL.replace("TB_KEYWORD", keyWord);
        Connection.Response
                response = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Mobile Safari/537.36")
                .header("authority", "suggest.taobao.com")
                .header("method", "GET")
                .header("path", "/sug?q=asd&code=utf-8&area=c2c&nick=&sid=null&callback=jsonp1")
                .header("scheme", "https")
                .header("accept", "*/*")
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                .header("accept-encoding", "gzip, deflate, br")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cookie", "cna=/l9LFMzrFXICAXTVqJD1KK4J; t=3cac68bab4151650662fa6cd7fda8f2e; thw=cn; mt=ci%3D-1_0; enc=8AhJZw2XaAKOOkWIYplA9K10T3gSI4bxlofWXdJ2qsnofuPiSvxV%2Fb4akg5DZMdKzz2j0pQgD3HytdQAx9D7QQ%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; v=0; cookie2=141df0e6c1565d317163f07b13173034; _tb_token_=e434d1718f59f; _m_h5_tk=e76167def701e643d3743fcfa771b4cf_1539778039920; _m_h5_tk_enc=c9d0287a87428a1a2552d9a46120354d; isg=BICAfBziz4S8vbOCal82g4ddUQ6SoWTRqWutGPoRTRsvdSGfohmmY0YDiZsQRRyr")
                .header("referer", "https://s.m.taobao.com/h5?sst=2&n=20&buying=buyitnow&q="+keyWord)
                .method(Connection.Method.POST)
                .ignoreContentType(true)
                .timeout(10000)
                .execute();
        String body = response.body();
        body = body.substring(body.indexOf("{"), body.lastIndexOf("}") + 1);
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONArray result = jsonObject.getJSONArray("result");
        for (int i = 0; i < result.size(); i++) {
            JSONArray arr = result.getJSONArray(i);
            String key = arr.getString(0);
            list.add(key);
        }
        return list;
    }

    /**
     * 根据rankType获取不同的热门搜索
     * @param params
     * @return
     * @throws IOException
     */
    public static List hotWord(Map<String,Object> params) throws IOException {
        Set<String> set=new HashSet<String>();
        String keyWord = params.get("keyWord")+"";
        String[] keyWords=keyWord.split(",");
        String[] rankTypes=IPlatformConstant.RANK_TYPE_LIST.split(",");
        for(int i=0;i<rankTypes.length;i++){
            List hotWords=getHotWord(keyWords[keyWords.length-1],rankTypes[i]);
            set.addAll(hotWords);
        }
        return new ArrayList(set);
    }

    /**
     * 获取热门搜索词
     * @param keyWord
     * @param rankType
     */
    public static List getHotWord(String keyWord,String rankType) throws IOException {
        List<String> list = new ArrayList<>();
        String url = IPlatformConstant.TAOBAO_H5_HOT_WORD_URL.replace("KEYWORD", keyWord).replace("RAKNTYPE",rankType);
        Connection.Response
                response = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
                .header("method", "GET")
                .header("accept", "*/*")
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("accept-encoding", "gzip, deflate")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cookie", "JSESSIONID=C0xY4Ta-1UOa9oC3w06BfZ5AxI-CrJNn6R-r4Fq3; cookie2=1387edb4182a370eac47a39eb223816f; t=5004aec0376b53561feb45887026a3ff; _tb_token_=ee353155eb54; __cn_logon__=false; _tmp_ck_0=%2BaYyUIQBRUz1Hb57Dtb0fRrpNjJQie1JFChmLNv27cE9g2IFqTIG8uclQy4iLMMusFwT8inDzqjRK0mcnrYHfVBq%2F9FAJn%2Fdl9jpepfVMDcAUu6eM1KSPztQkVfM4IOTpNoph%2BTUvS7%2F3TQ4mxSUVFW8R0yhju2Gt2RM1gyT%2FZ6%2BizWMRi1lVeMeO2a2EdWzRMdpZuWgx3p%2B1sLb0Q32veptcatjKBvGmzCiO8Av9S0h3N%2FROUod56b5IOP6iz%2FJDk%2FdIuhLQ6TuUfDZtd1DNEGLpBo8adoISt0prPp%2BirKa1rEnfJYf6A0bInm%2B3D0vRe0YkYDmQTdEHGPczf86s4rPjWTuBFRhVLnbs9H6Zn3BOOslyBqRJ45vMOlWGaeT; _alizs_base_attr_type_=0; _alizs_area_info_=1001; _alizs_user_type_=purchaser; ali_beacon_id=116.213.168.144.1539832757338.471108.8; cna=telOFO/oCzACAXTVqJDCTM9h; ali_ab=116.213.168.144.1539833304819.0; h_keys=\"sk2%u795e%u4ed9%u6c34#2gn3073320\"; ad_prefer=\"2018/10/18 14:06:40\"; alicnweb=touch_tb_at%3D1539853516968; _alizs_cate_info_=97%252C1043498%252C1043500; isg=BOfnyt3NcDZLT_S1UjK244mOdhtxxLpL-2qQGLlULXadqAdqwTztniXpzuiTQJPG")
                .header("referer", "http://index.1688.com/alizs/keyword.htm?userType=purchaser&cat="+keyWord)
                .method(Connection.Method.POST)
                .ignoreContentType(true)
                .timeout(10000)
                .execute();
        String body = response.body();
        body = body.substring(body.indexOf("{"), body.lastIndexOf("}") + 1);
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONArray content = jsonObject.getJSONArray("content");
        System.out.println(content);
        for (int i = 0; i < content.size(); i++) {
            JSONObject obj = content.getJSONObject(i);
            String key = obj.getString("keyword");
            list.add(key);
        }
        return list;
    }
    @Test
    public void test() throws UnsupportedEncodingException {
        HashMap<Object, Object> params = new HashMap<>();
        params.put("keyWord","测试");
        params.put("pageSize","50");
        //设置必要参数
        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl证书支持
        dcaps.setCapability("acceptSslCerts", true);
        //截屏支持
        dcaps.setCapability("takesScreenshot", true);
        //css搜索支持
        dcaps.setCapability("cssSelectorsEnabled", true);
        //js支持
        dcaps.setJavascriptEnabled(true);
        //驱动支持（第二参数表明的是你的phantomjs引擎所在的路径，使用whereis phantomjs可以查看）
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\dev\\tools\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
        //创建无界面浏览器对象
        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        //设置隐性等待
        driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String url = IPlatformConstant.TAOBAO_ULAND_SEARCH_URL.replace("TB_KEYWORD",URLEncoder.encode(params.get("keyWord")+"","utf-8")).replace("PAGENUM",params.get("pageNum")+"");
        //获取内容
        driver.get(url);
        System.out.println("===start0================="+url);
        TaobaoListItem taobaoListItem;
        List<WebElement> list = driver.findElementsByClassName("item");
        List<TaobaoListItem> listItem = new ArrayList<>();
        int start = 0;
        int num = Integer.parseInt(StringUtils.isBlank(params.get("pageSize")+"")?"50":params.get("pageSize")+"");
        for (WebElement item:list ) {
            if(start++ > num){
                break;
            }
            taobaoListItem = new TaobaoListItem();
            String goodsUrl = item.findElements(By.tagName("a")).get(0).getAttribute("href");
            String price = item.findElements(By.tagName("strong")).get(0).getText();
            String title = item.findElements(By.className("title")).get(0).getAttribute("title");
            System.out.println(title);
            List<WebElement> shopNick1 = item.findElements(By.className("shopNick"));
            String shopNick = "";
            if(!shopNick1.isEmpty()){
                shopNick = item.findElements(By.className("shopNick")).get(0).getText();
            }
            String payNum = item.findElements(By.className("payNum")).get(0).getText();
            String imgUrl = item.findElements(By.tagName("img")).get(0).getAttribute("src");
            List<WebElement> elementList = item.findElements(By.className("postalicon"));
            taobaoListItem.setName(title);
            taobaoListItem.setUrl(goodsUrl);
            taobaoListItem.setDescription(title);
            taobaoListItem.setPrice(price);
            taobaoListItem.setStoreName(shopNick);
            taobaoListItem.setPayersNum(payNum);
            taobaoListItem.setImgUrl(imgUrl);
            if(elementList.isEmpty()){
                taobaoListItem.setByShipping(2);
            }else{
                taobaoListItem.setByShipping(1);
            }
            listItem.add(taobaoListItem);
        }
        System.out.println("抓取到多个商品："+list.size());
    }
}
